package com.huhu.myharmony.utils;

import ohos.global.i18n.utils.TextRecognitionUtils;

import java.math.BigInteger;
import java.util.Locale;

public class DataUtils {

    /**
     * 将int转为高字节在前，低字节在后的byte数组（大端）
     *
     * @param n int
     * @return byte[]
     */
    public static byte[] intToByteBig(int n) {
        byte[] b = new byte[4];
        for (int i = 0; i < b.length; i++) {
            b[i] = (byte) (n >> (8 * (b.length - 1 - i)) & 0xff);//&0xff表示只取一个字节
        }
        return b;
    }

    /**
     * 将int转为低字节在前，高字节在后的byte数组（小端）
     *
     * @param n int
     * @return byte[]
     */
    public static byte[] intToByteLittle(int n) {
        byte[] b = new byte[4];
        for (int i = 0; i < b.length; i++) {
            b[i] = (byte) (n >> (8 * i) & 0xff);//&0xff表示只取一个字节
        }
        return b;
    }

    /**
     * byte数组到int的转换(小端)
     *
     * @param bytes
     * @return
     */
    public static int bytes2IntLittle(byte[] bytes) {
        int ret = 0;
        int count = Math.min(4, bytes.length);
        for (int i = 0; i < count; i++) {
            ret |= ((short) bytes[i] & 0xff) << (8 * i);
        }
        return ret;
    }

    /**
     * byte数组到int的转换(大端)
     * 0x1234 存储方式
     * 0 - 0x12
     * 1 - 0x34
     *
     * @param bytes
     * @return
     */
    public static int bytes2IntBig(byte... bytes) {
        int ret = 0;
        int count = Math.min(4, bytes.length);
        for (int i = 0; i < count; i++) {
            ret |= ((short) bytes[i] & 0xff) << (8 * (count - 1 - i));
        }
        return ret;
    }

    /**
     * 将short转为高字节在前，低字节在后的byte数组（大端）
     *
     * @param n short
     * @return byte[]
     */
    public static byte[] shortToByteBig(short n) {
        byte[] b = new byte[2];
        for (int i = 0; i < b.length; i++) {
            b[i] = (byte) (n >> (8 * (b.length - 1 - i)) & 0xff);//&0xff表示只取一个字节
        }
        return b;
    }

    /**
     * 将short转为低字节在前，高字节在后的byte数组(小端)
     *
     * @param n short
     * @return byte[]
     */
    public static byte[] shortToByteLittle(short n) {
        byte[] b = new byte[2];
        for (int i = 0; i < b.length; i++) {
            b[i] = (byte) (n >> (8 * i) & 0xff);//&0xff表示只取一个字节
        }
        return b;
    }

    /**
     * 读取小端byte数组为short
     *
     * @param bytes
     * @return
     */
    public static short byteToShortLittle(byte[] bytes) {
        short ret = 0;
        int count = Math.min(2, bytes.length);
        for (int i = 0; i < count; i++) {
            ret |= ((short) bytes[i] & 0xff) << (8 * (i));
        }
        return ret;
    }

    /**
     * 读取大端byte数组为short
     *
     * @param bytes
     * @return
     */
    public static short byteToShortBig(byte[] bytes) {
        short ret = 0;
        int count = Math.min(2, bytes.length);
        for (int i = 0; i < count; i++) {
            ret |= ((short) bytes[i] & 0xff) << (8 * (count - 1 - i));
        }
        return ret;
    }

    /**
     * long类型转byte[] (大端)
     *
     * @param n
     * @return
     */
    public static byte[] longToBytesBig(long n) {
        byte[] b = new byte[8];
        for (int i = 0; i < 8; i++) {
            b[i] = (byte) (n >> (8 * (8 - 1 - i)) & 0xff);
        }
        return b;
    }

    /**
     * long类型转byte[] (小端)
     *
     * @param n
     * @return
     */
    public static byte[] longToBytesLittle(long n) {
        byte[] b = new byte[8];
        for (int i = 0; i < 8; i++) {
            b[i] = (byte) (n >> (8 * i) & 0xff);//&0xff表示只取一个字节
        }
        return b;
    }

    /**
     * byte[]转long类型(小端)
     * 0x1234
     * 3412
     *
     * @param array
     * @return
     */
    public static long bytesToLongLittle(byte[] array) {
        long ret = 0;
        for (int i = 0; i < 8; i++) {
            ret |= ((long) array[i] & 0xff) << (8 * i);
        }
        return ret;
    }

    /**
     * byte[]转long类型(大端)
     *
     * @param array
     * @return
     */
    public static long bytesToLongBig(byte[] array) {
        long ret = 0;
        for (int i = 0; i < 8; i++) {
            ret |= ((long) array[i] & 0xff) << (8 * (8 - 1 - i));
        }
        return ret;
    }

    public static String byte2HexStr(byte[] b) {
        if (b == null) {
            return "";
        } else {
            StringBuilder hs = new StringBuilder();
            byte[] var3 = b;
            int var4 = b.length;

            for (int var5 = 0; var5 < var4; ++var5) {
                byte aB = var3[var5];
                int a = aB & 255;
                String stmp = Integer.toHexString(a);
                if (stmp.length() == 1) {
                    hs.append("0").append(stmp);
                } else {
                    hs.append(stmp);
                }

                hs.append(" ");
            }

            return hs.toString();
        }
    }

    //將16進制字符串轉換為10進制數字
    public static int decodeHEX(String hexs) {
        BigInteger bigint = new BigInteger(hexs, 16);
        return bigint.intValue();
    }

    //將16進制字符串轉換為10進制數字
    public static float decodeHEX2(String hexs) {
        BigInteger bigint = new BigInteger(hexs, 16);
        return bigint.floatValue();
    }


    /**
     * byte数组转换成16进制字符串
     *
     * @return 如果bytes为null则返回null，如果bytes长度为0返回""，其他返回正常转换的字符串
     */
    public static String toHex(byte[] bytes) {
        return toHex(bytes, " ");
    }

    /**
     * byte数组转换成16进制字符串
     *
     * @param separator 用来分隔的字符串
     * @return 如果bytes为null则返回null，如果bytes长度为0返回""，其他返回正常转换的字符串
     */
    public static String toHex(byte[] bytes, String separator) {
        if (bytes == null) {
            return null;
        } else if (bytes.length == 0) {
            return "";
        }
        StringBuilder sb = new StringBuilder();
        for (byte aSrc : bytes) {
            int v = aSrc & 0xFF;
            String hv = Integer.toHexString(v);
            if (hv.length() < 2) {
                sb.append(0);
            }
            sb.append(hv);
            if (separator != null) {
                sb.append(separator);
            }
        }
        String s = sb.toString().toUpperCase(Locale.ENGLISH);
        if (separator != null) {
            s = s.substring(0, s.length() - separator.length());
        }
        return s;
    }

    /**
     * 补零
     *
     * @param src       原字符串
     * @param targetLen 目标长度
     * @param head      补前面还是后面
     */
    public static String fillZero(String src, int targetLen, boolean head) {
        if (src == null) return null;
        StringBuilder sb = new StringBuilder(src);
        while (sb.length() % targetLen != 0) {
            if (head) {
                sb.insert(0, "0");
            } else {
                sb.append("0");
            }
        }
        return sb.toString();
    }

    /**
     * 数字转16进制字符串，不足2位自动补零
     */
    public static String encodeHex(int num) {
        return encodeHex(num, 2, true);
    }

    public static String encodeHex(int num, int targetLen) {
        return encodeHex(num, targetLen, true);
    }

    public static String encodeHex(int num, int targetLen, boolean head) {
        return fillZero(Integer.toHexString(num), targetLen, head);
    }

    /**
     * 16进制求和
     *
     * @param hexStr 16进制字符串
     * @return 16进制字符串
     */
    public static String makeChecksum(String hexStr) {
        if (hexStr == null || hexStr.equals("")) {
            return "00";
        }
        hexStr = hexStr.replaceAll(" ", "");
        int total = 0;
        int len = hexStr.length();
        if (len % 2 != 0) {
            return "00";
        }
        int num = 0;
        while (num < len) {
            String s = hexStr.substring(num, num + 2);
            total += Integer.parseInt(s, 16);
            num = num + 2;
        }
        return hexInt(total);
    }

    private static String hexInt(int total) {
        int a = total / 256;
        int b = total % 256;
        if (a > 255) {
            return hexInt(a) + format(b);
        }
        return format(a) + format(b);
    }

    private static String format(int hex) {
        String hexa = Integer.toHexString(hex);
        int len = hexa.length();
        if (len < 2) {
            hexa = "0" + hexa;
        }
        return hexa;
    }


    /**
     * 进16进制字符串转换成字节数组
     *
     * @param hexStr    16进制的字符串
     * @param separator 字符串字节间的分隔符
     */
    public static byte[] toByteArray(String hexStr, String separator) {
        String s = hexStr.replaceAll(separator, "");
        if (s.length() % 2 != 0) {
            s = "0" + s;
        }
        byte[] bytes = new byte[s.length() / 2];
        for (int i = 0; i < bytes.length; i++) {
            bytes[i] = (byte) Integer.parseInt(s.substring(i * 2, i * 2 + 2), 16);
        }
        return bytes;
    }
}
